/**
 * 分类管理模块
 * @author HuHaiqin
 */
layui.define([
    'jquery',
    'tree',
    'layer',
    'upload'
], function(exports) {

    var $ = layui.jquery;
    var catadmin = {
        "loadTreeData": function() {
            //先初始化
            $('#catTree').html("");
            var $this = this;
            var kechengID = $('#kechengID').val();
            if (kechengID) {
                var dataUrl = '/kecheng/mulu/getdata_cat_tree/kecheng_id/' + kechengID;
                $.get(dataUrl, function(ret) {
                    if (ret.status) {
                        layui.tree({
                            elem: '#catTree',
                            nodes: ret.data,
                            click: function(node) {
                                //console.log(node) //node即为当前点击的节点数据
                                var catId = node.id;
                                var curActionType = $('#action-type-btns button.layui-btn-danger').attr('data-type');
                                var iframeUrl = "";
                                if (curActionType == 'del') {
                                    var asker = layer.confirm('确定删除分类"' + node.name + '"吗？', function() {
                                        var ii;
                                        $.ajax({
                                            url: '/kecheng/mulu/del/ids/' + catId,
                                            type: "get",
                                            dataType: "json",
                                            beforeSend: function() {
                                                ii = layer.msg('Loading...', { icon: 16, time: 10000 });
                                            },
                                            complete: function() {
                                                layer.close(ii);
                                            },
                                            success: function(data) {
                                                if (data.status) {
                                                    layer.msg('删除成功', { icon: 1, time: 1000 }, function() {
                                                        iframeUrl = '/kecheng/mulu/edit/kecheng_id/' + kechengID;
                                                        $('iframe#catEditFrame').attr('src', iframeUrl);
                                                        $this.loadTreeData();
                                                    });
                                                } else {
                                                    layer.msg(data.info, { icon: 2, time: 1000 });
                                                }
                                            }
                                        });
                                    });
                                } else if (curActionType == 'edit') {
                                    iframeUrl = '/kecheng/mulu/edit/id/' + catId;
                                    $('iframe#catEditFrame').attr('src', iframeUrl);
                                } else {
                                    iframeUrl = '/kecheng/mulu/edit/parentid/' + catId;
                                    $('iframe#catEditFrame').attr('src', iframeUrl);
                                }
                            }
                        });

                    } else {
                        layer.alert(ret.info);
                    }
                });
            }
        }
    };

    layui.upload.render({
        url: '/api/upload/qiniu?type=image',
        elem: '#fengmianupload',
        ext: 'jpg|png|gif|bmp|jpeg',
        method: 'post',
        before: function() {
            ii = layer.msg('图片上传中...', {
                icon: 16,
                time: 10000
            });
        },
        done: function(ret) {
            if (ret.status) {
                change_fengmian(ret.data.url);
                layer.close(ii);
            } else {
                layer.alert(ret.info);
            }
        }
    });

    //监听封面图改变
    $('#fengmian').change(function() {
        change_fengmian($('#fengmian').val());
    });

    function change_fengmian(url) {
        $('#fengmian').val(url);
        $('#fengmian-box').html('<img src="' + url + '">');
        $('#fengmian-box').unbind('click').click(function() {
            var $this = $(this);
            //调用示例
            layer.photos({
                photos: '#fengmian-box',
                anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            });
        });
    }

    $('#action-type-btns button').click(function() {
        var $this = $(this);
        if ($this.hasClass('layui-btn-danger')) {
            //不做处理
        } else {
            $('#action-type-btns button').removeClass('layui-btn-danger');
            $this.addClass('layui-btn-danger');
            if ($this.data('type') == 'add' || $this.data('type') == 'del') {
                var kechengID = $('#kechengID').val();
                var iframeUrl = '/kecheng/mulu/edit/kecheng_id/' + kechengID;
                $('iframe#catEditFrame').attr('src', iframeUrl);
            }
        }
    });

    exports('muluadmin', catadmin); //对外接口
});